package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.BillQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.BillDao;
import cn.org.eshow.model.Bill;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("billDao")
public class BillDaoHibernate extends GenericDaoHibernate<Bill, Integer> implements BillDao {

    public BillDaoHibernate() {
        super(Bill.class);
    }

    @Override
    public List<Bill> list(BillQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<Bill> search(BillQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(BillQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Bill.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, BillQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (StringUtils.isNotEmpty(query.no)) {
                rule.add(Restrictions.like("no", query.no, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.category)) {
                rule.add(Restrictions.like("category", query.category, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.type)) {
                rule.add(Restrictions.like("type", query.type, MatchMode.ANYWHERE));
            }
            if (query.minReceive != null) {
                rule.add(Restrictions.ge("receive", query.minReceive));
            }
            if (query.maxReceive != null) {
                rule.add(Restrictions.le("receive", query.maxReceive));
            }
            if (query.minOutlay != null) {
                rule.add(Restrictions.ge("outlay", query.minOutlay));
            }
            if (query.maxOutlay != null) {
                rule.add(Restrictions.le("outlay", query.maxOutlay));
            }
            if (query.minBalance != null) {
                rule.add(Restrictions.ge("balance", query.minBalance));
            }
            if (query.maxBalance != null) {
                rule.add(Restrictions.le("balance", query.maxBalance));
            }
            if (query.img != null) {
                rule.add(Restrictions.like("img", query.img, MatchMode.ANYWHERE));
            }
            if (query.orderType != null) {
                rule.add(Restrictions.like("orderType", query.orderType, MatchMode.ANYWHERE));
            }
            if (query.orderNo != null) {
                rule.add(Restrictions.like("orderNo", query.orderNo, MatchMode.ANYWHERE));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (query.remark != null) {
                rule.add(Restrictions.like("remark", query.remark,
                        MatchMode.ANYWHERE));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
